package com.lansi.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import com.lansi.bean.User;
import com.lansi.mapper.UserMapper;
import com.lansi.service.ex.PasswordNotMatchException;
import com.lansi.service.ex.UserNotFoundException;
import com.lansi.service.ex.UsernameAlreadyExistException;

@Service
public class UserService implements IUserService{
	@Resource
	private UserMapper userMapper ;
	private String salt = "蓝思?";
	
	//1.用户注册
	public void register(User user) {
		//1.调用持久层selectUserByUsername
		User user1 = userMapper.selectUserByUsername(user.getUsername());
		
		//2.判断
		if(user1==null) { 
			//明文密码
			String pwd = user.getUserkey();
			//生成密文
			String pwdMd5 = DigestUtils.md5Hex(pwd+salt);
			//user对象中设置密文
			user.setUserkey(pwdMd5);
			
			userMapper.insertUser(user);
		}else {
			throw new UsernameAlreadyExistException("用户名已存在");
		}
	}
	//2.用户信息的修改
	public void updateUser(
			Integer id, String username,
			String sn,
			String tel,String usermail,	
			Date date,String prio) {
		
		User newUser= new User();
		newUser.setId(id);
		//newUser.setUsername(username);
		//newUser.setSn(sn);
		//newUser.setUserkey(userkey);//密码
		newUser.setTel(tel);
		newUser.setUsermail(usermail);
		newUser.setCtime(date);		
		newUser.setPrio(prio);	
		
		//1.调用持久层方法selectUserById(id);返回对象
		User user = userMapper.selectUserById(id);
		if(user==null) {
			//2.user==null抛出UserNotFoundException
			throw new UserNotFoundException("用户不存在");
		}else {
			//3.user!=null  调用selectUserByUsername(username);返回user1对象
			User user1=userMapper.selectUserByUsername(username);
			User user2=userMapper.selectByPhone(tel);
			User user3=userMapper.selectByEmail(usermail);
			System.out.println("user1"+user1+"user2"+user2+"user3"+user3);
			if(user1!=null) {
				//当前用户名就是登录的用户名
				if(user1.getUsername().equals(user.getUsername())) {
					
				}else {
					throw new UsernameAlreadyExistException("用户名已存在");
				}
			}else {
				//5.newUser==null；调用持久层方法updateUser(new User)
				newUser.setUsername(username);			
			}
			System.out.println(newUser);
			
			userMapper.updateUser(newUser);
		}
			
	}
	
	//3.修改密码
		public void changePassword(Integer id, String oldPwd, String newPwd) {
			//1.调持久层方法
			User user = userMapper.selectUserById(id);
			if(user==null) {
				throw new UserNotFoundException("用户不存在");
			}else {			
				//把oldpwd变成密文
				String oldMd5 = DigestUtils.md5Hex(oldPwd+salt);
				
				String pw = user.getUserkey();
				if(pw.equals(oldMd5)) {
					 User user1 = new User();
					//把oldpwd变成密文
					 String newMd5 = DigestUtils.md5Hex(newPwd+salt);
					 user1.setUserkey(newMd5);;
					 user1.setId(id);
					 userMapper.updateUser(user1);
				}else {
					throw new PasswordNotMatchException("密码不匹配");
				}
			}
			
		}

	//验证邮箱是否存在邮箱
	public boolean checkEmail(String email) {
		User num = userMapper.selectByEmail(email);
		if(num!=null) {
			return true;//存在
		}else {
			return false;//不存在
		}
		
	}

	public boolean checkPhone(String phone) {
		User num = userMapper.selectByPhone(phone);
		if(num!=null) {
			return true;
		}else {
			return false;
		}
	}

	public boolean checkUsername(String username) {
		System.out.println("UserService.checkUsername");
		User user = userMapper.selectUserByUsername(username);
		if(user!=null) {
			return true;
		}else {
			return false;
		}
	}
	
	//用户登录
	public User login(String loginaccount,String key,String savekey) {
		System.out.println("UserService.login");
		User user=null;
		//手机
		if(loginaccount!=null  && loginaccount.matches("^1[0-9]{10}$")) {
			user = userMapper.selectByPhone(loginaccount);
		   }
		//用户名
		else if(loginaccount!=null && loginaccount.matches("[A-Z,a-z,0-9]*")) {
			//1.调用持久层方法
			 user = userMapper.selectUserByUsername(loginaccount);
			 System.out.println(loginaccount);
		}		
		//邮箱
		else if(loginaccount!=null && loginaccount.contains("@") && loginaccount.contains(".")) {
			 user = userMapper.selectByEmail(loginaccount);
		}
		
		if(user==null) {
			throw new UserNotFoundException("用户不存在");
		}else {
			String pw=user.getUserkey();
			//把密码处理成密文和数据库的密文进行比较
			String pwdMd5 = DigestUtils.md5Hex(key+salt);
			
			if(pw.equals(pwdMd5)) {
				return user;
			}else {
				throw new PasswordNotMatchException("密码不匹配");
			}
			
		}
	}
	
	
	//根据id查询用户
	public User getUserById(Integer id) {
		
		return userMapper.selectUserById(id);
	}
	@Override
	public void updateImage(String image, Integer id) {
		// TODO Auto-generated method stub
		
	}
	//根据id删除用户信息-可以批量删除
	@Override
	public void delUser(Integer[] ids) {
		userMapper.deleteUser(ids);
		
	}
	//单个删除用户，根据id
	@Override
	public void delById(Integer id) {
		userMapper.deleteById(id);
		
	}
	//查询出所有的用户信息
	@Override
	public List<User> getUser(Integer offset,Integer count) {
		
		return userMapper.selectUser(offset, count);
	}
	
	//根据用户名查询
	@Override
	public User getUsername(String username) {
		
		return userMapper.selectUserByUsername(username);
	}
	//根据手机查询
	@Override
	public User getTel(String tel) {
		
		return userMapper.selectByPhone(tel);
	}
	//根据邮箱查询
	@Override
	public User getUsermail(String usermail) {
		
		return userMapper.selectByEmail(usermail);
	}
	//查询数据库用户数量
	@Override
	public Integer getUsercount() {
		
		return userMapper.selectUsercount();
	}
	

	

	
}
